<%@ page language="java" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
%>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.User,
                 com.scalar.chiptrack.commons.SessionKeys"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>


<html>

<HEAD>
<TITLE>ScalarSoft - ChipTrac - Wafer Data Entry </TITLE>
<META http-equiv='Content-Type' content='text/html; charset=windows-1252'>
<LINK href='<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css' type='text/css' rel='stylesheet'>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script language='javascript' >
var waferListSize = 0;

function resetValues()
{
    document.waferDataEntryForm.reset();
    calculateTotal();
}

function closeWindow()
{
    window.close();
}
function onLoad()
{
    var totalQty = 0;

    var rowIndex = <%=request.getParameter("rowIndex")%>;

    var form		            = document.waferDataEntryForm;
    var openerForm	            = window.opener.document.forms[0];
    var tableIdCtrl = document.getElementById("waferInfoTableId");
    var qtyInStr        = openerForm["selectedLotsList[" + rowIndex +"].binQtyAsString"].value;
    var waferCheckedStr = openerForm["selectedLotsList[" + rowIndex +"].waferChecked"].value;
    var waferIdsStr    = openerForm["selectedLotsList[" + rowIndex +"].waferIds"].value;
    var waferIds = waferIdsStr.split(";");

    var binQty    = null;
    var checkValue    = null;

    if ( qtyInStr != null && qtyInStr != '' )
    {
        binQty = qtyInStr.split(";");
    }

    if ( waferCheckedStr != null && waferCheckedStr != '' )
    {
        checkValue = waferCheckedStr.split(";");
    }

    for( var i=0; i<waferIds.length; i++ )
    {
        if ( waferIds[i] != '' )
        {
            //alert( "checkValue" + checkValue );
            var newRow = tableIdCtrl.insertRow( eval(tableIdCtrl.rows.length-1 ) );
            newRow.align = "center";
            var newCell = newRow.insertCell();
            newCell.style.backgroundColor = "#DEE1E2";
            if ( checkValue != null )
            {
                if( checkValue[i] == "false" )
                {
                    var htmltext = '<input type="checkbox" name="waferChecked" onClick= "javascript:calculateTotal();" >';
                }
                else
                {
                    var htmltext = '<input type="checkbox" name="waferChecked" checked onClick= "javascript:calculateTotal();" >';
                }
            }
            else
            {
                var htmltext = '<input type="checkbox" name="waferChecked" checked onClick= "javascript:calculateTotal();" >';
            }
            newCell.innerHTML = htmltext;
            newCell = newRow.insertCell();
            newCell.style.backgroundColor = "#DEE1E2";
            htmltext = '<input type="text" name="waferId" value="'+waferIds[i]+'" size=6 class="txfield" disabled="true">';
            newCell.innerHTML = htmltext;
            newCell = newRow.insertCell();
            newCell.style.backgroundColor = "#DEE1E2";
            htmltext = '<input type="text" name="binQty" value='+ binQty[i] +' size=6 class="valuetxfield" onkeypress="javascript:allowInteger()" onchange= "javascript:calculateTotal();">';
            newCell.innerHTML = htmltext;
            if ( checkValue != null )
            {
                if( checkValue[i] == "true" )
                {
                     totalQty += parseInt( binQty[i] );
                }
            }
            else
            {
                totalQty += parseInt( binQty[i] );
            }

            waferListSize ++;
        }
        form.runningTotal.value = totalQty;
    }

}

function calculateTotal()
{
   var form = document.waferDataEntryForm;
   var totalQty = 0;

    if ( waferListSize > 1 )
    {
        for ( var i=0; i < waferListSize; i++ )
        {
            if ( form.binQty[i].value != '' )
            {
                if ( form.waferChecked[i].checked == true )
                {
                    if ( form.binQty[i].disabled == true )
                    {
                        form.binQty[i].disabled       = false;
                    }
                    totalQty += parseInt( form.binQty[i].value );
                }
                else
                {
                    form.binQty[i].disabled = true;
                }

            }
        }
    }
    else
    {
       if ( form.waferChecked.checked == true )
       {
            totalQty = parseInt( form.binQty.value );
       }
    }

    form.runningTotal.value = totalQty;
}

function save()
{
    var pa  = document.getElementById("errors");
    pa.innerHTML = '';

    var rowIndex = <%=request.getParameter("rowIndex")%>;
    var form		            = document.waferDataEntryForm;
    var openerForm	            = window.opener.document.forms[0];
    var dieQtyInCtrl            = openerForm["selectedLotsList[" + rowIndex +"].shipQty"];
    var checkedBinQtyCtrl       = openerForm["selectedLotsList[" + rowIndex +"].checkedBinQtyAsString"];
    var checkedWaferIdsCtrl     = openerForm["selectedLotsList[" + rowIndex +"].checkedWaferIdsAsString"];
    var binqtyInCtrl            = openerForm["selectedLotsList[" + rowIndex +"].binQtyAsString"];
    var waferCheckedStrCtrl     = openerForm["selectedLotsList[" + rowIndex +"].waferChecked"];
    var waferIdsStr             = openerForm["selectedLotsList[" + rowIndex +"].waferIds"].value;

    var waferIds = waferIdsStr.split(";");
    var waferCheckedStr     = '';
    var binQtyInStr         = '';
    var checkedWaferIdsStr  = '';
    var checkedBinQtyStr    = '';

    if ( waferListSize > 1  )
    {
        for ( var k=0; k < waferListSize; k++ )
        {
            if( form.binQty[k].value == '' || !isInteger( form.binQty[k].value ) )
            {
                pa.innerHTML = "<li> Please enter Valid Die Quantity for wafer id :" + waferIds[k];
                form.binQty[k].focus();
                return;
            }
           if ( form.waferChecked[k].checked == true )
           {
               checkedBinQtyStr     = (checkedBinQtyStr != '' ? checkedBinQtyStr + ";" + form.binQty[k].value : form.binQty[k].value);
               checkedWaferIdsStr   = (checkedWaferIdsStr != '' ? checkedWaferIdsStr + ";" + form.waferId[k].value : form.waferId[k].value);
           }
           waferCheckedStr = (k==0 ? form.waferChecked[k].checked  : waferCheckedStr + ";" + form.waferChecked[k].checked  )
           binQtyInStr     = (k==0 ? form.binQty[k].value : binQtyInStr + ";" + form.binQty[k].value);
        }
     }
     else
     {
        if( form.binQty.value == '' || !isInteger( form.binQty.value ) )
        {
            pa.innerHTML = "<li> Please enter Valid Die Quantity for wafer id :" + waferIds;
            form.binQty.focus();
            return;
        }
        if ( form.waferChecked.checked == true)
        {
            checkedBinQtyStr        = form.binQty.value;
            checkedWaferIdsStr      = form.waferId.value;
        }
        else
        {
            pa.innerHTML = "<li> Please select wafer id :" + waferIds;
            return;
        }

        binQtyInStr             = form.binQty.value;
        waferCheckedStr         = form.waferChecked.checked;
     }

     checkedBinQtyCtrl.value    = checkedBinQtyStr;
     binqtyInCtrl.value         = binQtyInStr;
     checkedWaferIdsCtrl.value  = checkedWaferIdsStr;
     waferCheckedStrCtrl.value  = waferCheckedStr;

     dieQtyInCtrl.value         = form.runningTotal.value;

     closeWindow();
}

</script>

</HEAD>
<%
    User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
    if ( user != null )
    {
%>
<BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' onLoad='onLoad()'>
<form name="waferDataEntryForm" action="">
<TABLE align='center' border='0' width='100%' >
    <TR>
        <td align='center' class="NArialW">DieBank Wafer Info</td>
    </TR>
    <TR>
        <TD align='center'>&nbsp;</TD>
        <TR><TD align="right"><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></TD></TR>
    <tr><td align="center" colspan="10" class='NArialL'><FONT id="errors" color="#FF0000"></FONT></td></tr>
    <TR>
        <TD id = "tderrors" class='NArialL' style="color:green;" align = 'center'>
          <chiptrack:info styleClass="NArialL" />
          <chiptrack:errors styleClass="NArialL" />
        </TD>
    </TR>
    <TR>
        <TD align='center'>
			<TABLE id= "waferInfoTableId"align="center" border="0" width="35%">
            <tr bgcolor="#993333" align = "center">
               <td align = "center" class='NArialL' nowrap><font color="#FFFFFF">Select</font></td>
               <td align = "center" class='NArialL' nowrap><font color="#FFFFFF">Wafer Id</font></td>
               <td align = "center" class='NArialL' nowrap><font color="#FFFFFF">Qty In</font></td>
            </tr>
            <tr align = "center">
               <td>&nbsp;</td>
               <td align = "right" class='NArialL' nowrap>Running Total</td>
               <td align = "right" class='NArialL' nowrap><font color="#FFFFFF"><input type= "text" name="runningTotal" size ="12" class="valuetxfield" disabled="true"></font></td>
            </tr>
           </TABLE>
         </TD>
    </TR>

    <TR><TD align='center'>&nbsp;</TD></TR>
    <TR>
            <TD align='center'>
                <a href="javascript:save()" title='Click here to Save Wafer Data Entry'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" ></a>&nbsp;&nbsp;&nbsp;
                <a href="javascript:resetValues()" title='Click here to reset all field values'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" ></a>&nbsp;&nbsp;&nbsp;
                <a href="javascript:window.close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a>
            </TD>
    </TR>
</TABLE>
</form>
</body>
<%
    }
    else
    {
%>
    <BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' >
    <TABLE cellpadding='0' cellspacing='0' bgcolor='FDF5E6' border="0" width="100%"  height="100%" align=center>
        <TR>
            <TD>
                <TABLE align="center" border="0" width="40%" bgcolor="#FDF5E6">
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><font class="NArialL" ><font color="red"><b>Session has been time out , Please close this window and login again.</b></font></font></TD></TR>
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></TD></TR>
                </TABLE>
            </TD>
       </TR>
       <TR><TD>&nbsp;</TD></TR>
    </TABLE>
    </BODY>
<% } %>
</html>
